﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CTRL;
using DAL;
using System.Threading;

namespace GUI_Administration
{
    public partial class GUICustomer : Form
    {
        private CtrlCustomer ctrlCust;
        private static GUICustomer instance;

        private GUICustomer()
        {
            ctrlCust = CtrlCustomer.Instance;

            InitializeComponent();

            UpdateTable();
        }

        public static GUICustomer Instance
        {
            get
            {
                if (instance == null)
                    instance = new GUICustomer();

                return instance;
            }
        }

        private void UpdateTable()
        {
            dataGridView.Rows.Clear();
            List<Customer> list = ctrlCust.GetAllCustomers().ToList();
            foreach (Customer cust in list)
                dataGridView.Rows.Add(cust.customerID, cust.name, cust.username, cust.password, cust.email);
        }

        private void getByNameBtn_Click(object sender, EventArgs e)
        {
            if (nameTxtBox.Text.ToString() == "0")
                UpdateTable();
            else
            {
                dataGridView.Rows.Clear();
                Customer _cust = ctrlCust.GetCustomerByName(nameTxtBox.Text.ToString());
                dataGridView.Rows.Add(_cust.customerID, _cust.name, _cust.username, _cust.password, _cust.email);
            }
            nameTxtBox.Clear();
        }

        private void getByIDBtn_Click(object sender, EventArgs e)
        {
            if (int.Parse(IDtxtBox.Text) == 0)
                UpdateTable();
            else
            {
                dataGridView.Rows.Clear();
                Customer _cust = ctrlCust.GetCustomer(int.Parse(IDtxtBox.Text));
                dataGridView.Rows.Add(_cust.customerID, _cust.name, _cust.username, _cust.password, _cust.email);
            }
            IDtxtBox.Clear();
        }

        private void addBtn_Click(object sender, EventArgs e)
        {
            string name = dataGridView.CurrentRow.Cells[1].Value.ToString();
            string username = dataGridView.CurrentRow.Cells[2].Value.ToString();
            string password = dataGridView.CurrentRow.Cells[3].Value.ToString();
            string email = dataGridView.CurrentRow.Cells[4].Value.ToString();
            ctrlCust.CreateCustomer(name, username, password, email);
            UpdateTable();
        }

        private void updateBtn_Click(object sender, EventArgs e)
        {
            int ID = int.Parse(dataGridView.CurrentRow.Cells[0].Value.ToString());
            string name = dataGridView.CurrentRow.Cells[1].Value.ToString();
            string username = dataGridView.CurrentRow.Cells[2].Value.ToString();
            string password = dataGridView.CurrentRow.Cells[3].Value.ToString();
            string email = dataGridView.CurrentRow.Cells[4].Value.ToString();
            ctrlCust.UpdateCustomer(ID, name, username, password, email);
            UpdateTable();
        }

        private void deleteBtn_Click(object sender, EventArgs e)
        {
            ctrlCust.DeleteCustomer(int.Parse(dataGridView.CurrentRow.Cells[0].Value.ToString()));
            UpdateTable();
        }

        private void back_Click(object sender, EventArgs e)
        {
            GUIMain main = GUIMain.Instance;
            main.Show();

            this.Hide();
        }
    }
}